package com.lkj.framework.netty;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lkj.framework.config.HandlerControllerConfig;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author likanjia
 */
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<String> {

    @Autowired
    private HandlerControllerConfig config;

    @Override
    protected void channelRead0(ChannelHandlerContext context, String msg) throws Exception {

        log.info("msg:"+msg);

        JSONObject object = JSONUtil.parseObj(msg);
        String url = object.getStr("url");
        String param = object.getStr("param");

//        HandlerControllerConfig config = new HandlerControllerConfig();
        config.doGetMapper(url,param);
//        HandlerControllerConfig.getInstance().doGetMapper(url,param);

        Channel channel = context.channel();
        System.out.println(channel.remoteAddress());
        channel.writeAndFlush("Hello world");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        System.out.println(ctx.channel().remoteAddress()+"上线了...");

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        System.out.println(ctx.channel().remoteAddress()+"离线了....");

    }
}
